package com.qkl.xiatang.app.dto.feed;

import com.qkl.xiatang.app.dto.pond.PondGroupDTO;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.appenum.FeedAllocStatusEnum;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 添加动保
 */
@Data
public class ZooAllocAddDTO {

    private List<String> groupIdList;

    private List<String> pondIdList;

    /**
     * yyyy-MM-dd
     */
    private String time;

    private String feedId;

    private String feedName;

    private Double qty;

    private List<FeedAddDTO> feedList;

    public ZooFeedDTO toZooFeedDTO(){
        List<String> feedIdList = new ArrayList<>();
        List<FeedAddDTO> feedAddDTOList = new ArrayList<>();
        if(ObjectUtils.isNotEmpty(feedList)){
            for(FeedAddDTO feedAddDTO : feedList){
                if(StringUtils.isEmpty(feedAddDTO.getFeedId())){
                    continue;
                }
                feedIdList.add(feedAddDTO.getFeedId());
                feedAddDTOList.add(feedAddDTO);
            }
        }else {
            feedIdList.add(feedId);
            FeedAddDTO feedAddDTO = new FeedAddDTO();
            feedAddDTO.setFeedId(feedId);
            feedAddDTO.setQty(qty);
            feedAddDTOList.add(feedAddDTO);
        }
        ZooFeedDTO zooFeedDTO = new ZooFeedDTO();
        zooFeedDTO.setFeedIdList(feedIdList);
        zooFeedDTO.setFeedList(feedAddDTOList);
        return zooFeedDTO;
    }


    public boolean checkParams(){
        groupIdList = AppUtils.stringsDistinct(groupIdList);
        pondIdList = AppUtils.stringsDistinct(pondIdList);
        if(ObjectUtils.isEmpty(groupIdList) && ObjectUtils.isEmpty(pondIdList)){
            return false;
        }
        feedId = StringUtils.trimToEmpty(feedId);
        if(StringUtils.isAnyEmpty(feedId) && ObjectUtils.isEmpty(feedList) ){
            return false;
        }

        time = StringUtils.trimToEmpty(time);
        if(StringUtils.isAnyEmpty(time)){
            return false;
        }
        time = AppUtils.toYYYY_MM_DD(time);
        if(!AppUtils.checkYyyyMMdd(time)){
            return false;
        }
        if(time.compareTo(AppUtils.yyyyMMdd()) < 0){
            return false;
        }

        if( (null == qty || qty <= 0) && ObjectUtils.isEmpty(feedList) ){
            return false;
        }
        return true;
    }

    public List<ZooAllocDetailDTO> toDetailByGroup(String userId , String userName , String companyId, Date allocDate,List<PondGroupDTO> pondGroupDTOS){
        List<ZooAllocDetailDTO> detailDTOS = new ArrayList<>();
        ZooFeedDTO zooFeedDTO = this.toZooFeedDTO();
        List<FeedAddDTO> feedAddDTOList = zooFeedDTO.getFeedList();
        for(int i = 0 , size = feedAddDTOList.size(); i < size ; i++ ){
            FeedAddDTO addDTO = feedAddDTOList.get(i);
            Map<String,String> map = new HashMap<>();
            for(PondGroupDTO groupDTO : pondGroupDTOS) {
                String groupId = groupDTO.getGroupId();
                if (!map.containsKey(groupId)) {
                    map.put(groupId, AppUtils.genIdStr() + i);
                }
                detailDTOS.add(this.genDetail(map.get(groupId), groupDTO.getGroupName(), userId, userName, allocDate, groupDTO.getPondId(), companyId, addDTO.getQty(), addDTO.getFeedId()));
            }
        }
        return detailDTOS;
    }

    public List<ZooAllocDetailDTO> toDetailByPondIds(String userId , String userName ,String companyId, Date allocDate,List<String> pondIdList){
        List<ZooAllocDetailDTO> detailDTOS = new ArrayList<>();

        ZooFeedDTO zooFeedDTO = this.toZooFeedDTO();
        List<FeedAddDTO> feedAddDTOList = zooFeedDTO.getFeedList();
        for(FeedAddDTO addDTO : feedAddDTOList){
            String zooGroupId = AppUtils.genIdStr();
            String groupName = "自定义";
            for(String pondId : pondIdList) {
                detailDTOS.add(this.genDetail(zooGroupId, groupName, userId, userName, allocDate, pondId, companyId, addDTO.getQty(), addDTO.getFeedId()));
            }
        }

        return detailDTOS;
    }

    private ZooAllocDetailDTO genDetail(String zooGroupId,String groupName,String userId,String userName,Date allocDate,String pondId,String companyId,Double qty,String feedId){
        qty = AppUtils.formatAmt(qty);

        ZooAllocDetailDTO detailDTO = new ZooAllocDetailDTO();
        detailDTO.setId(AppUtils.genIdStr(detailDTO));
        detailDTO.setZooGroupId(zooGroupId);
        detailDTO.setZooGroupName(groupName);
        detailDTO.setAllocUserId(userId);
        detailDTO.setAllocUserName(userName);
        detailDTO.setAllocDate(allocDate);
        detailDTO.setPondId(pondId);
        detailDTO.setCompanyId(companyId);
        detailDTO.setName("动保");
        detailDTO.setQty(qty);
        detailDTO.setFeedId(feedId);
        detailDTO.setStatus(FeedAllocStatusEnum.NOT.getStatus());
        if(qty == 0.0){
            detailDTO.setStatus(FeedAllocStatusEnum.UN_NEED.getStatus());
        }
        return detailDTO;
    }


    @Data
    public static  class ZooFeedDTO{

        private List<String> feedIdList;

        private List<FeedAddDTO> feedList;


    }




}
